load(":ndk_linux_arm64_toolchain_config.bzl", "ndk_cc_toolchain_config")
load(":clang_linux_amd64_toolchain_config.bzl", "provide_linux_amd64_toolchain_config")
load(":mac_toolchain_config.bzl", "provide_mac_toolchain_config")

package(default_visibility = ["//visibility:public"])

toolchain(
    name = "linux_amd64_clang_toolchain",
    # Where should we run this toolchain?
    exec_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    # What can this toolchain build?
    target_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
        # We want to be able to explicitly tell Bazel to use this toolchain, and not the
        # default one on a user's machine or on the RBE worker. Thus we need an extra constraint
        # that we can use to differentiate the "stock" C++ toolchain from our hermetic one and
        # force that use by specifying the target platform.
        "//bazel/platform:use_hermetic_toolchain",
    ],
    toolchain = ":linux_amd64_clang",
    # https://github.com/bazelbuild/rules_cc/blob/8bb0eb5c5ccd96b91753bb112096bb6993d16d13/cc/BUILD#L32-L36
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

toolchain(
    name = "linux_amd64_ndk_arm64_toolchain",
    exec_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:android",
        "@platforms//cpu:arm64",
    ],
    toolchain = ":linux_amd64_ndk_arm64",
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

toolchain(
    name = "linux_amd64_ndk_arm32_toolchain",
    exec_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:android",
        "@platforms//cpu:armv7",
    ],
    toolchain = ":linux_amd64_ndk_arm32",
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

filegroup(name = "not_implemented")

###################################
# clang on linux amd64 toolchain. #
###################################

provide_linux_amd64_toolchain_config(
    name = "linux_amd64_toolchain_config",
)

filegroup(
    name = "archive_linux_amd64_files",
    srcs = [
        "linux_amd64_trampolines/ar.sh",
        "@clang_linux_amd64//:archive_files",
    ],
)

filegroup(
    name = "compile_linux_amd64_files",
    srcs = [
        "linux_amd64_trampolines/clang.sh",
        "@clang_linux_amd64//:compile_files",
    ],
)

filegroup(
    name = "link_linux_amd64_files",
    srcs = [
        # Bazel assumes it is talking to Clang when linking.
        "linux_amd64_trampolines/clang.sh",
        "@clang_linux_amd64//:link_files",
    ],
)

cc_toolchain(
    name = "linux_amd64_clang",
    all_files = ":not_implemented",
    ar_files = ":archive_linux_amd64_files",
    compiler_files = ":compile_linux_amd64_files",
    dwp_files = ":not_implemented",
    linker_files = ":link_linux_amd64_files",
    objcopy_files = ":not_implemented",
    strip_files = ":not_implemented",
    supports_param_files = False,
    toolchain_config = ":linux_amd64_toolchain_config",
)

############################
# arm64-v8a C++ toolchain. #
############################

# https://bazel.build/reference/be/c-cpp#cc_toolchain
cc_toolchain(
    name = "linux_amd64_ndk_arm64",
    all_files = ":not_implemented",
    ar_files = ":ndk_arm64_v8a_toolchain_all_files",
    compiler_files = ":ndk_arm64_v8a_toolchain_all_files",
    dwp_files = ":not_implemented",
    dynamic_runtime_lib = "@ndk_linux_amd64//:arm64_v8a_dynamic_runtime_libraries",
    linker_files = ":ndk_arm64_v8a_toolchain_all_files",
    objcopy_files = ":not_implemented",
    static_runtime_lib = "@ndk_linux_amd64//:arm64_v8a_static_runtime_libraries",
    strip_files = ":not_implemented",
    supports_param_files = False,
    toolchain_config = ":ndk_arm64_v8a_toolchain_config",
)

filegroup(
    name = "ndk_arm64_v8a_toolchain_all_files",
    srcs = glob(["android_trampolines/*.sh"]) + ["@ndk_linux_amd64//:arm64_v8a_all_files"],
)

ndk_cc_toolchain_config(
    name = "ndk_arm64_v8a_toolchain_config",
    cpu = "arm64-v8a",
)

##############################
# armeabi-v7a C++ toolchain. #
##############################

# https://bazel.build/reference/be/c-cpp#cc_toolchain
cc_toolchain(
    name = "linux_amd64_ndk_arm32",
    all_files = ":not_implemented",
    ar_files = ":ndk_armeabi_v7a_toolchain_all_files",
    compiler_files = ":ndk_armeabi_v7a_toolchain_all_files",
    dwp_files = ":not_implemented",
    dynamic_runtime_lib = "@ndk_linux_amd64//:armeabi_v7a_dynamic_runtime_libraries",
    linker_files = ":ndk_armeabi_v7a_toolchain_all_files",
    objcopy_files = ":not_implemented",
    static_runtime_lib = "@ndk_linux_amd64//:armeabi_v7a_static_runtime_libraries",
    strip_files = ":not_implemented",
    supports_param_files = False,
    toolchain_config = ":ndk_armeabi_v7a_toolchain_config",
)

filegroup(
    name = "ndk_armeabi_v7a_toolchain_all_files",
    srcs = glob(["android_trampolines/*.sh"]) + ["@ndk_linux_amd64//:armeabi_v7a_all_files"],
)

ndk_cc_toolchain_config(
    name = "ndk_armeabi_v7a_toolchain_config",
    cpu = "armeabi-v7a",
)

###########################################
# clang on Mac (Intel and Apple silicon). #
###########################################

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#37.
#
# Our one mac toolchain can run on either Intel Macs or M1 Macs, however Bazel does not allow you to specify
# more than one cpu type in exec_compatible_with. Thus, we list the same toolchain twice.
toolchain(
    name = "clang_mac_x64_toolchain",
    exec_compatible_with = [
        "@platforms//os:macos",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:macos",
        "@platforms//cpu:x86_64",
        "//bazel/platform:use_hermetic_toolchain",
    ],
    toolchain = ":mac_host",
    # https://github.com/bazelbuild/rules_cc/blob/8bb0eb5c5ccd96b91753bb112096bb6993d16d13/cc/BUILD#L32-L36
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#55
toolchain(
    name = "clang_mac_arm64_toolchain",
    exec_compatible_with = [
        "@platforms//os:macos",
        # The Skia repository uses cpu:arm64, but as of 2023-10-25 that yields a "No matching
        # toolchains found" error on Apple silicon. It could be due to the fact that the Skia and
        # skcms repositories use different Bazel versions.
        "@platforms//cpu:aarch64",
    ],
    target_compatible_with = [
        "@platforms//os:macos",
        # The Skia repository uses cpu:arm64, but as of 2023-10-25 that yields a "No matching
        # toolchains found" error on Apple silicon. It could be due to the fact that the Skia and
        # skcms repositories use different Bazel versions.
        "@platforms//cpu:aarch64",
        "//bazel/platform:use_hermetic_toolchain",
    ],
    toolchain = ":mac_host",
    # https://github.com/bazelbuild/rules_cc/blob/8bb0eb5c5ccd96b91753bb112096bb6993d16d13/cc/BUILD#L32-L36
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#71.
#
# Cross compilation toolchain for an Intel Mac host and Apple silicon target
toolchain(
    name = "clang_host_mac_x64_target_mac_arm64_toolchain",
    exec_compatible_with = [
        "@platforms//os:macos",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:macos",
        # The Skia repository uses cpu:arm64, but as of 2023-10-25 that yields a "No matching
        # toolchains found" error on Apple silicon. It could be due to the fact that the Skia and
        # skcms repositories use different Bazel versions.
        "@platforms//cpu:aarch64",
        "//bazel/platform:use_hermetic_toolchain",
    ],
    toolchain = ":mac_target_arm64",
    # https://github.com/bazelbuild/rules_cc/blob/8bb0eb5c5ccd96b91753bb112096bb6993d16d13/cc/BUILD#L32-L36
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#148.
filegroup(
    name = "archive_mac_files",
    srcs = [
        "mac_trampolines/ar_trampoline_mac.sh",
        "@clang_mac//:archive_files",
    ],
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#156.
filegroup(
    name = "compile_mac_files",
    srcs = [
        "mac_trampolines/clang_trampoline_mac.sh",
        "@clang_mac//:compile_files",
    ],
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#164.
filegroup(
    name = "link_mac_files",
    srcs = [
        "mac_trampolines/clang_trampoline_mac.sh",
        "@clang_mac//:link_files",
    ],
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#181.
filegroup(
    name = "all_mac_files",
    srcs = [
        ":archive_mac_files",
        ":compile_mac_files",
        ":link_mac_files",
    ],
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#194.
provide_mac_toolchain_config(
    name = "mac_toolchain_config",
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#198.
provide_mac_toolchain_config(
    name = "mac_arm64_toolchain_config",
    cpu = "arm64",
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#220.
cc_toolchain(
    name = "mac_host",
    all_files = ":all_mac_files",  # Apparently also used to compile objc code
    ar_files = ":archive_mac_files",
    compiler_files = ":compile_mac_files",
    dwp_files = ":not_implemented",
    linker_files = ":link_mac_files",
    module_map = select({
        "@platforms//os:macos": "@clang_mac//:generated_module_map",
        "//conditions:default": None,
    }),
    objcopy_files = ":not_implemented",
    strip_files = ":not_implemented",
    supports_param_files = False,
    toolchain_config = ":mac_toolchain_config",
)

# Based on
# https://skia.googlesource.com/skia/+/0d4fcf388a6f9318e2a54fa85fddf3396d521767/toolchain/BUILD.bazel#234.
cc_toolchain(
    name = "mac_target_arm64",
    all_files = ":all_mac_files",  # Apparently also used to compile objc code
    ar_files = ":archive_mac_files",
    compiler_files = ":compile_mac_files",
    dwp_files = ":not_implemented",
    linker_files = ":link_mac_files",
    module_map = select({
        "@platforms//os:macos": "@clang_mac//:generated_module_map",
        "//conditions:default": None,
    }),
    objcopy_files = ":not_implemented",
    strip_files = ":not_implemented",
    supports_param_files = False,
    toolchain_config = ":mac_arm64_toolchain_config",
)
